﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;


public enum InputType
{
    Normal,
    Password,
    Disable
};

public class CmdLineControl : MonoBehaviour {
    // 绑定的控件
    public InputFieldEx m_InputFieldEx;          // 命令输入框
    public Text m_TextShow;               // 历史命令显示框
    public Transform m_UIPanel;
    public Transform m_UIMaskView;

    // 使用的内部变量
    int m_UpdateScrollOffsetCount = 0;

    // 保存注册在本类中的所有程序
    Dictionary<string, CmdLineProcess> m_ProcessList;
    CmdLineProcess m_CurrentProcess;

    RectTransform m_UIPanelRect;
    RectTransform m_UIMaskViewRect;
    RectTransform m_TextShowRect;
    RectTransform m_InputFieldExRect;

    public void UpdateScrollOffset()
    {
        // 这个是将scrollview拉到最底部
        {
            // 滑动到最下方
            float a = m_UIPanelRect.rect.height;
            float b = m_UIMaskViewRect.rect.height;

            if ((a - b) > 0)
            {
                m_UIPanel.localPosition = new Vector3(0, a - b, 0);
            }
            else
            {
                m_UIPanel.localPosition = new Vector3(0, 0.01f, 0);
            }
        }

        // 不知为何VerityLayout的计算么次在换行的时候会计算错误，所有这里手动计算一下
        {
            if(m_TextShowRect.localPosition.y + m_TextShowRect.rect.height != m_InputFieldExRect.localPosition.y)
            {
                m_TextShowRect.localPosition = new Vector3(0, m_InputFieldExRect.localPosition.y - m_TextShowRect.rect.height, 0);
            }
        }
    }

    public void OnValueChanged(object sender, string InputCmd, string FullLine)
    {
        // 这个复制表明在接下来的三帧中，都需要刷新
        m_UpdateScrollOffsetCount = 3;
        // 刷新SCROLLVIEW
        UpdateScrollOffset();
    }

    public void OnCmdEnter(object sender, string InputCmd, string FullLine)
    {
        SetInfo(FullLine + "\r\n");

        if (m_CurrentProcess != null){
            m_CurrentProcess.InputCmdLine(InputCmd);
        }
    }

    // Use this for initialization
    void Start ()
    {
        if (m_InputFieldEx)
        {
            //OnValueChanged;
            m_InputFieldEx.ValueChangeEvent += OnValueChanged;
            m_InputFieldEx.CmdEnterEvent += OnCmdEnter;
            m_TextShowRect = m_InputFieldEx.GetComponent<RectTransform>();            
        }

        if(m_TextShow)
        {
            m_InputFieldExRect = m_TextShow.GetComponent<RectTransform>();
        }

        if (m_UIPanel)
        {
            m_UIPanelRect = m_UIPanel.GetComponent<RectTransform>();
        }

        if (m_UIMaskView)
        {
            m_UIMaskViewRect = m_UIMaskView.GetComponent<RectTransform>();
        }
    }

    static Boolean bPer = false;

    // Update is called once per frame
    void Update ()
    {
		if(m_UpdateScrollOffsetCount > 0)
        {
            m_UpdateScrollOffsetCount--;
            UpdateScrollOffset();
        }
    }

    // 设置信息
    public void SetInfo(string strInfo)
    {
        if (strInfo.EndsWith("\r\n"))
        {
            // 创建新对象
            Text NewTextShow = GameObject.Instantiate(m_TextShow, m_UIPanel);
            NewTextShow.text = strInfo.Substring(0, strInfo.Length - 2);
            NewTextShow.transform.SetSiblingIndex(m_UIPanel.childCount - 2);
            m_InputFieldExRect = NewTextShow.GetComponent<RectTransform>();
        }
        else
        {
            m_InputFieldEx.SetTip(strInfo);
        }
        // 这个复制表明在接下来的三帧中，都需要刷新
        m_UpdateScrollOffsetCount = 3;
        // 刷新SCROLLVIEW
        UpdateScrollOffset();
    }


    public void SetInputType(InputType type)
    {
        m_InputFieldEx.SetInputType(type);
    }

    // 增加程序列表
    public void RegisterProcess(string strProcessName, CmdLineProcess RegsiterProcess)
    {
        if (m_ProcessList == null)
        {
            m_ProcessList = new Dictionary<string, CmdLineProcess>();
        }

        m_ProcessList[strProcessName] = RegsiterProcess;
    }

    // 设置当前用于接收的程序
    public void SetCurrentProcess(string strProcessName)
    {
        if(m_CurrentProcess != null)
        {
            m_CurrentProcess.LeaveFromProcess("");
        }
        m_CurrentProcess = m_ProcessList[strProcessName];
        if(m_CurrentProcess != null)
        {
            m_CurrentProcess.EnterIntoProcess("");
        }
    }
}
